package com.lanhai.longheng.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
 
import com.lanhai.longheng.interfaces.ICmdLog;

/**
 * 协议记录
 * @author zhengwei
 *
 */
@Service
public class CmdLogImpl implements ICmdLog{
 
	 private static final Logger agvlog =  LogManager.getLogger("agvlog");
	 private static final Logger devicelog = LogManager.getLogger("device");  
	  
	 static Map<Integer,String> agvReceiveMap;
	 static {
		 agvReceiveMap=Collections.synchronizedMap(new HashMap<Integer,String>());
	 }
	 
	 
	//写文件
	
	@Override
	public synchronized void addAgvSendLog(int agvid, String remark, String dataStr) { 
		 
		
		String log=String.format("AGV:【%d】，发送消息:%s，消息内容:【%s】", agvid,remark,dataStr);
		agvlog.info(log);
		System.out.println(log);
	}

	@Override
	public synchronized void addAgvReceiveLog(int agvid, String remark, String dataStr) {
		//过滤相同的指令，只是不做记录，不影响处理
		String last=agvReceiveMap.get(agvid);
		if(last!=null&&last.equals(dataStr)) {
			return;
		}
		agvReceiveMap.put(agvid, dataStr);
		
		 
		String log=String.format("收到来自AGV:【%d】的消息:【%s】，消息内容:【%s】", agvid,remark,dataStr);
		agvlog.info(log);
		System.out.println(log);
	}

	@Override
	public synchronized void addDeviceSendLog(String ip,int port,int line, String remark, String dataStr,String devicename) {
	 	 
		
		String log=String.format("对接设备IP:【%s】,线别:【%d】，发送消息:%s，消息内容:【%s】",devicename,  line,remark,dataStr);
		devicelog.info(log);
		System.out.println(log);
 }

	@Override
	public synchronized void addDeviceReceiveLog(String ip,int line, String remark, String dataStr,String devicename) {
		 
		
		String log=String.format("收到对接设备IP:【%s】,线别:【%d】的消息:【%s】，消息内容:【%s】", devicename,line,remark,dataStr);
		devicelog.info(log);
		System.out.println(log);
	}

}
